#pragma once

#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;

class Mutex
{
public:
    Mutex(pthread_mutex_t *lock_p = nullptr)
        : _lock_p(lock_p)
    {
    }
    void lock()
    {
        if (_lock_p) // 锁不为空，才表示要设置锁
            pthread_mutex_lock(_lock_p);
    }
    void unlock()
    {
        if (_lock_p) // 锁不为空，表示有锁需要我们解锁
            pthread_mutex_unlock(_lock_p);
    }
    ~Mutex() {}

private:
    pthread_mutex_t *_lock_p; //////////////////////我们这里没有锁，需要外面传锁进来
};

class LockGuard
{
public:
    LockGuard(Mutex mutex)
        : _mutex(mutex)
    {
        _mutex.lock(); // 在构造函数中加锁
    }
    ~LockGuard()
    {
        _mutex.unlock(); // 在析构函数中解锁
    }

private:
    Mutex _mutex;
};
